package com.leafant.gametemplate;

import java.util.Random;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MainSurfaceView extends SurfaceView  implements SurfaceHolder.Callback, Runnable{

	private boolean m_is_running;
	private SurfaceHolder m_surfaceholder;
	private static int s_screen_width, s_screen_height;
	private Paint m_paint;
	private Thread m_mainthread;
	private String m_string_to_draw;
	private int m_string_x, m_string_y;
	private Canvas m_lockcanvas;
	private float m_touch_x, m_touch_y;
	private Random m_random;
	Rect m_bounds;
	
	public MainSurfaceView(Context context) {
		super(context);
		m_surfaceholder = getHolder();
		m_surfaceholder.addCallback(this);		
		setFocusable(true);
		s_screen_width = context.getResources().getDisplayMetrics().widthPixels;
		s_screen_height = context.getResources().getDisplayMetrics().heightPixels;		
	}

	private void Initialize(){
		
		m_touch_x = -1;
		m_touch_y = -1;
		m_string_x = s_screen_width/2;
		m_string_y = s_screen_height/2;
		m_paint = new Paint();
		m_random = new Random();
		m_bounds = new Rect();
		m_string_to_draw = "Touch Me";
		m_paint.getTextBounds(m_string_to_draw, 0, m_string_to_draw.length(), m_bounds);
		m_mainthread = new Thread(this);
		m_mainthread.start();
		m_is_running = true;
		
		
	}
	
	public void GameCreate(){

	}
	
	public void GameStart(){

	}
	
	public void GamePause(){

	}
	
	public void GameResume(){

	}
	
	public void GameStop(){

	}
	
	public void GameDestroy(){

	}
	
	public void GameRestart(){

	}
	
	private void GameUpdate(){
		
	}
	
	private void GameDraw(Canvas canvas){
		canvas.drawColor(Color.WHITE);		
		m_paint.setColor(Color.DKGRAY);
		canvas.drawRect((float)(m_string_x - m_bounds.right/2), (float)(m_string_y - Math.abs(m_bounds.top)*2), (float)(m_string_x - m_bounds.right/2)+ (float)(m_bounds.right*2), (float)(m_string_y - Math.abs(m_bounds.top)*2) + (float)(Math.abs(m_bounds.top)*3), m_paint);
		m_paint.setColor(Color.YELLOW);
		canvas.drawText(m_string_to_draw, m_string_x, m_string_y, m_paint);
		m_paint.setColor(Color.BLACK);
		canvas.drawText("Touch X: "+ m_touch_x, 15, 20, m_paint);
		canvas.drawText("Touch Y: "+ m_touch_y, 15, 35, m_paint);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent e){
		super.onTouchEvent(e);
		m_touch_x = e.getX();
		m_touch_y = e.getY();
		if(CheckPointInRect(Math.round(m_touch_x), Math.round(m_touch_y), m_string_x - m_bounds.right/2, m_string_y - Math.abs(m_bounds.top)*2, m_bounds.right*2, Math.abs(m_bounds.top)*3)){
			m_string_x = m_random.nextInt(s_screen_width - m_bounds.right*2) + m_bounds.right/2;
			m_string_y = m_random.nextInt(s_screen_height - Math.abs(m_bounds.top)*3) + Math.abs(m_bounds.top) + Math.abs(m_bounds.top)/2;
		}
		return true;
	}
	
	private boolean CheckPointInRect(int px, int py, int rx, int ry, int rw, int rh){
		if(px >= rx && px <= (rx + rw) && py >= ry && py <= (ry + rh))
			return true;
		return false;
	}

	@Override
	public void run() {
		while(m_is_running){
			try{
				// Thread.sleep(20);
			}catch(Exception e){}
			GameUpdate();
			m_lockcanvas = m_surfaceholder.lockCanvas(null);
			synchronized (m_surfaceholder) {
				if(m_lockcanvas != null){
					GameDraw(m_lockcanvas);
					m_surfaceholder.unlockCanvasAndPost(m_lockcanvas);
				}
			}
		}		
	}

	@Override
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void surfaceCreated(SurfaceHolder arg0) {
		Initialize();		
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder arg0) {
		boolean retry = true;
		m_is_running = false;
        do {
            try {
                m_mainthread.join();
                retry = false;
            } catch (InterruptedException e) {
				e.printStackTrace();
                // we will try it again and again...
            }
        }while(retry);		
	}
}
